require(['../config'], function() {
  require(['jquery', 'avalon', 'util', 'loan/product-apply', 'page','Cookies','text!/common/nav.html','text!/common/footer.html','navInit'], function(
    $,
    avalon,
    util,
    productApply,
    paging,Cookies,
    navHtml,
    footerHtml,
    navInit
  ) {
    $(".global-nav").html(navHtml);
    $(".global-footer").html(footerHtml);
    navInit();

    var page1 = paging()
    var productId = util.getUrlParam('productId')
    var managerId = util.getUrlParam('id')
    if (!productId || !managerId) {
      location.href = '/loan/product.html'
    }

    var vm = avalon.define({
      $id: 'channel-info',
      data: {},
      products: {},
      productId: productId,
      managerId: managerId,
      pageItems: [1, 2, 3, 4, 5],
      params: {
        page: 0,
        limit: 6
      },
      rate: '',
      avatar: function() {
        return this.data.avatar || '/assets/images/product/channel.png'
      },
      imgUrl: function(item) {
        return item.logo || 'http://img0.rongsue.com/images/channel/rec-logo.png'
      },
      productName: function(item) {
        return item.instName + '-' + item.name
      },
      info: function(item) {
        return item.takeTimesText + '内放款，利率' + item.rateMin + '%'
      },
      loadData: function() {
        var data = vm.$model
        util
          .ajax({
            url: '/loan/product/manager/' + this.managerId
          })
          .then(function(rsp) {
            vm.data = rsp.data
          })
      },
      queryProductList: function() {
        util
          .ajax({
            url: '/loan/product/' + this.managerId + '/list'
          })
          .then(function(rsp) {
            vm.products = rsp.data
          })
      },
      apply: function() {
        productApply.apply(this.managerId, this.productId)
      }
    })

    // 渠道评价
    var commentVm = avalon.define({
      $id: 'comment-list',
      data: {},
      pageItems: [1, 2, 3, 4, 5],
      params: {
        page: 1,
        limit: 1
      },
      avatar: function(item) {
        return item.avatar || '/assets/images/user-default.jpg'
      },
      diaplayTime: function(time) {
        return util.diaplayTime(time)
      },
      star: function(score) {
        var list = []
        for (var i = score || 5; i--; ) {
          list.push('<i class="iconfont icon-star"></i>')
        }
        return list.join('')
      },
      queryCommentList: function() {
        var data = commentVm.$model
        util
          .ajax({
            url: '/loan/comment/manager/' + managerId + '/list',
            data: data.params
          })
          .then(function(rsp) {
            commentVm.data = rsp.data
            page1.init(commentVm.params.page, rsp.data.total)
            var sum = 0
            for (var i = 0, item; (item = rsp.data.rows[i++]); ) {
              sum += item.score
            }
            var rate = sum / rsp.data.rows.length
            if (rate) {
              vm.rate = rate + '分  （满分5分）'
            } else {
              vm.rate = '尚未有用户进行评分'
            }
          })
      }
    })

    avalon.scan(document.body)

    $(function() {
      vm.loadData()
      vm.queryProductList()
      commentVm.queryCommentList()

      page1.init(
        1,
        0,
        $('#page1'),
        function() {
          // 回调函数内可使用$(this).data('page')获取准备翻到的那一页
          commentVm.params.page = $(this).data('page')
          commentVm.queryCommentList()
        },
        commentVm.params.limit
      )

      var tabs = $('.tabs li a')
      tabs.click(function() {
        var content = $(this).data('ref')
        tabs.removeClass('active')
        $(this).addClass('active')
        $('#content')
          .find('.content-wrap')
          .hide()
        $('#' + content).fadeIn(200)
      })
    })
  })
})
